package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.*;
import com.zrwl.poscloud.base.enums.*;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.holder.RequestHolder;
import com.zrwl.poscloud.commons.utils.*;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.*;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.*;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 持卡人员 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-02-09
 */
@Service
public class EmployeeServiceImpl extends SuperServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeDeptRecordService employeeDeptRecordService;
    @Autowired
    private EmployeeRecordService employeeRecordService;
    @Autowired
    private CompanyAdminRService companyAdminRService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private AdminService adminService;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SequenceService sequenceService;
    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private EmployeeAccountRecordService employeeAccountRecordService;
    @Resource
    private CompanyDictDataMapper companyDictDataMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private UserEmployeeRMapper userEmployeeRMapper;

    @Autowired
    private ServerSyncEmployeeInfoService serverSyncEmployeeInfoService;

    @Override
    public IPage<EmployeeVO> getPageList(EmployeeVO employeeVO) {
        if (StringUtils.isNotEmpty(employeeVO.getCompanyCode()) && !StringUtils.isEmpty(employeeVO.getCompanyCode().trim())) {
            employeeVO.setCompanyCode(DeptUtil.getLatestCompnay(employeeVO.getCompanyCode()));
        } else {
            employeeVO.setCompanyCode(DeptUtil.getLatestCompnay(adminService.getMe().getCompanyCode()));
        }
        Page<EmployeeVO> page = new Page<>();
        page.setCurrent(employeeVO.getCurrentPage());
        page.setSize(employeeVO.getPageSize());
        IPage<EmployeeVO> pageList = employeeMapper.getPageList(page, employeeVO);
        List<EmployeeVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<EmployeeVO> getNameSakeList(EmployeeVO employeeVO) {
        if (StringUtils.isNotEmpty(employeeVO.getCompanyCode()) && !StringUtils.isEmpty(employeeVO.getCompanyCode().trim())) {
            employeeVO.setCompanyCode(DeptUtil.getLatestCompnay(employeeVO.getCompanyCode()));
        } else {
            employeeVO.setCompanyCode(DeptUtil.getLatestCompnay(adminService.getMe().getCompanyCode()));
        }
        Page<EmployeeVO> page = new Page<>();
        page.setCurrent(employeeVO.getCurrentPage());
        page.setSize(employeeVO.getPageSize());
        IPage<EmployeeVO> pageList = employeeMapper.getNameSakeList(page, employeeVO);
        List<EmployeeVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<EmployeeVO> getListForSelect(EmployeeVO employeeVO) {
        if (employeeVO.getKeyword() != null) {
            Page<EmployeeVO> page = new Page<>();
            page.setCurrent(employeeVO.getCurrentPage());
            page.setSize(employeeVO.getPageSize());
            IPage<EmployeeVO> pageList = employeeMapper.getListForSelect(page, employeeVO);
            List<EmployeeVO> list = pageList.getRecords();
            pageList.setRecords(list);
            return pageList;
        } else return null;
    }

    @Override
    public List<Employee> getAllList(EmployeeVO employeeVO) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<Employee> list = employeeService.list(queryWrapper);
        return list;
    }


    @Override
    public String add(EmployeeVO employeeVO) {
        Employee entity = new Employee();
        BeanUtils.copyProperties(employeeVO, entity, SysConf.STATUS);
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.setIsVip(0);//初始化标记VIP
        entity.insert();
        employeeVO.setUid(entity.getUid());
        employeeVO.setEmployeeCode(entity.getEmployeeCode());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }
    @Override
    public String edit(EmployeeVO employeeVO) {
        Employee entity = employeeService.getById(employeeVO.getUid());
        BeanUtils.copyProperties(employeeVO, entity, SysConf.CREATE_TIME);
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.updateById();
        serverSyncEmployeeInfoService.addSyncRecord(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(EmployeeVO employeeVO) {
        userEmployeeRMapper.unlink(employeeVO.getUid());
        Employee entity = employeeService.getById(employeeVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.updateById();

        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String batchLeave(List<EmployeeVO> employeeVOS) {
        int error = 0;
        for (EmployeeVO employeeVO : employeeVOS) {
            try {
                employeeService.leave(employeeVO);
            } catch (Exception e) {
                e.printStackTrace();
                if (e.getMessage() == "该持卡人已经离开") {
                    error += 1;
                }
            }
        }
        return ResultUtil.successWithMessage("成功注销" + (employeeVOS.size() - error) + ",注销失败" + error + "请确认账户余额清零后再次注销");
    }

    @Override
    public String leave(EmployeeVO employeeVO) {
        if (employeeVO.getUid() == null && employeeVO.getUid() != null) {
            employeeVO.setUid(employeeVO.getUid());
        }
        Employee entity = employeeService.getById(employeeVO.getUid());
        if (entity.getStatus() == 2) {
            throw new RuntimeException("该持卡人已经离开");
        }
        Date LeaveDate = new Date();
        entity.setStatus(EStatus.FREEZE);
        entity.setLeaveDate(LeaveDate);
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.updateById();
        BeanUtils.copyProperties(entity, employeeVO);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * @param list             文件解析后的数据
     * @param superCompanyCode 顶级机构
     * @param type             类型
     * @param payLevel         等级
     * @param ignoreDuplicates 身份证是否去重
     * @return 错误信息 errorList
     */
    @SneakyThrows
    @Override
    public List batchUploadEmployee(List<Map<String, String>> list, String superCompanyCode, Integer type, Integer payLevel, boolean ignoreDuplicates) {
        /***
         * 1.查询具有权限的所有机构 放到map中充当索引表    从map中判断是否存在 就不去数据库查了
         * 2.身份证号合法性校验,是否有相同身份证号的数据（根据前台的参数来选择性判重）
         * 3.校验非空字段
         *
         * 失败原因写入vo 并add到errorList中
         */
        //拿出顶级机构的对象
        List<CompanyAdminRVO> companyAdminRVOList = companyAdminRService.getMyCompanyList();
        DeptVO deptVO = new DeptVO();
        deptVO.setMyCompanyList(companyAdminRVOList);
        deptVO.setSuperCompanyCode(superCompanyCode);
        List<Dept> myDeptList = deptService.getAllList(deptVO);
        //用于查找是否存在机构
        Map<String, Dept> myDeptMap = new HashMap<String, Dept>();
        //用于表内判重
        Map<String, Object> successCertMap = new HashMap<String, Object>();
        for (Dept dept : myDeptList) {
            myDeptMap.put(dept.getName(), dept);
        }

        List<Map> errorList = new ArrayList<Map>();
        List<Employee> successList = new ArrayList<Employee>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).size() == 0) {
                continue;
            }
            Map<String, String> row = list.get(i);
            Employee employeeVO = new Employee();
            if (StringUtils.isEmpty(row.get("姓名"))) {
                row.put("失败原因", "姓名不能为空");
                errorList.add(row);
                continue;
            } else {
                employeeVO.setName(row.get("姓名"));
            }
            if (StringUtils.isEmpty(row.get("编号"))) {
                row.put("失败原因", "编号不能为空");
                errorList.add(row);
                continue;
            } else {
                employeeVO.setEmployeeCode(row.get("编号"));
            }
            //机构的有效性 根据前台传入的comCode，在dept表中寻找名称匹配的dept对象
            if (StringUtils.isEmpty(row.get("所属机构"))) {
                row.put("失败原因", "所属机构不能为空");
                errorList.add(row);
                continue;
            } else {
                Dept dept = myDeptMap.get(row.get("所属机构").trim());
                if (dept == null) {
                    row.put("失败原因", "所属机构不存在");
                    errorList.add(row);
                    continue;
                } else {
                    String mechanism = "";
                    //限制顶级机构
                    for (CompanyAdminRVO myCompanyList : deptVO.getMyCompanyList()) {
                        if (superCompanyCode.equals(myCompanyList.getCompanyCode())) {
                            mechanism = myCompanyList.getCompanyName();
                            if (dept.getCompanyUid().equals(myCompanyList.getCompanyUid())) {
                                employeeVO.setCompanyCode(dept.getCompanyCode());
                            }
                        }
                    }
                    if (employeeVO.getCompanyCode() == null) {
                        row.put("失败原因", "未从" + mechanism + "内查到该所属机构");
                        errorList.add(row);
                        continue;
                    }

                }
            }
            //判断身份证是否为空
            if (ignoreDuplicates && StringUtils.isNotEmpty(row.get("身份证号"))) {
                if (successCertMap.get(row.get("身份证号").trim()) != null) {
                    row.put("失败原因", "身份证号重复-表内重复");
                    errorList.add(row);
                    continue;
                }
                if (exitCertCode(superCompanyCode, row.get("身份证号"))) {
                    row.put("失败原因", "身份证号重复-数据库重复");
                    errorList.add(row);
                    continue;
                } else {
                    employeeVO.setCertCode(row.get("身份证号"));
                    //用于表内判重
                    successCertMap.put(employeeVO.getCertCode(), employeeVO);
                }
            } else {
                employeeVO.setCertCode(row.get("身份证号"));
            }
//            }
            if (!StringUtils.isEmpty(row.get("性别"))) {
                if (row.get("性别").equals("男")) {
                    employeeVO.setSex(1);
                } else if (row.get("性别").equals("女")) {
                    employeeVO.setSex(2);
                } else {
                    employeeVO.setSex(3);
                }
            } else {
                employeeVO.setSex(3);
            }
            if (!StringUtils.isEmpty(row.get("生日"))) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                employeeVO.setBirthday(formatter.parse((row.get("生日"))));
            }
            if (!StringUtils.isEmpty(row.get("进入时间"))) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                employeeVO.setEnterDate(formatter.parse(row.get("进入时间")));
            }
            //设置类型
            employeeVO.setType(type);
            //设置等级
            employeeVO.setPayLevel(payLevel);
            successList.add(employeeVO);
        }
        employeeService.saveBatch(successList);
        return errorList;
    }

    @Override
    public IPage<EmployeeVO> mobileUserGetList(EmployeeVO employeeVO) {
        Page<EmployeeVO> page = new Page<>();
        page.setCurrent(employeeVO.getCurrentPage());
        page.setSize(employeeVO.getPageSize());
        IPage<EmployeeVO> pageList = employeeMapper.mobileUserGetList(page, employeeVO);
        List<EmployeeVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public EmployeeAccountRecordVO getVerifyEmployee(EmployeeVO employeeVO) {
        EmployeeVO verifyEmployeeVO = employeeMapper.getVerifyEmployeeVO(employeeVO);
        EmployeeAccountRecordVO employeeAccountRecordVO = new EmployeeAccountRecordVO();
        return employeeAccountRecordService.employeeVOShiftEmployeeAccountRecordVO(employeeAccountRecordVO, employeeVO);
    }

    /**
     * 批量导入
     *
     * @param importDetailList   数据源
     * @param importBatch        数据批次
     * @param adminVO            管理员
     * @param columnConfigMap    列配置
     * @param payLevelList
     * @param sexList
     * @param deptRoleTreeEntity
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public int batchForImport(List<ImportDetail> importDetailList, ImportBatch importBatch, AdminVO adminVO, Map<String, String> columnConfigMap, List<CompanyDictData> payLevelList, List<SysDictDataVO> sexList, Map<String, DeptVO> deptRoleTreeEntity) {
        int error = 0;
        for (ImportDetail importDetail : importDetailList) {
            if (importDetail.getStatus() != 2) {
                try {
                    employeeService.singleImport(importDetail, importBatch, adminVO, columnConfigMap, payLevelList, sexList, deptRoleTreeEntity);
                    importDetail.setStatus(2);
                    importBatch.setImportedCount(importBatch.getImportedCount() + 1);
                } catch (RuntimeException e) {
                    importDetail.setSummary("错误原因 : " + e.getMessage());
                    importBatch.setSummary("第" + importDetail.getLineNumber() + "行中断,中断原因 :" + e.getMessage());
                    importDetail.setStatus(3);
                    error++;
                    break;
                }
            }
        }
        return error;
    }

    @Override
    public EmployeeVO employeeIsItEffective(Map<String, String> row, String companyCode, boolean whetherToCreate) {
        //插入人员(身份证号或编号,当前商户判重导入)
        EmployeeVO employeeVO = new EmployeeVO();
        employeeVO.setCompanyCode(companyCode);
        //确认姓名和编号不为空
        try {
            if (StringUtils.isEmpty(row.get("姓名"))) {
                throw new RuntimeException("姓名不能为空");
            }
            if (StringUtils.isEmpty(row.get("人员编号"))) {
                throw new RuntimeException("人员编号不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            row.put("失败原因", e.getMessage());
            employeeVO.setImportError(true);
            return employeeVO;
        }
        try {
            if (StringUtils.isNotEmpty(row.get("身份证号"))) {
                employeeVO.setCertCode(row.get("身份证号"));
                EmployeeVO certCodeTrans = employeeService.certCodeTrans(employeeVO);
                if (certCodeTrans.isImportError()) {
                    throw new RuntimeException("身份证号不正确");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            row.put("失败原因", e.getMessage());
            employeeVO.setImportError(true);
            return employeeVO;
        }
        //判断是否有该人员
        try {
            employeeVO.setEmployeeCode(row.get("人员编号"));
            EmployeeVO verify = employeeMapper.getVerifyEmployeeVO(employeeVO);
            if (verify == null) {
                if (StringUtils.isEmpty(row.get("身份证号"))) {
                    throw new RuntimeException("101");//身份证为空
                } else {
                    throw new RuntimeException("102");//没有该人员
                }
            }
            if (verify.getCertCode().isEmpty() && StringUtils.isNotEmpty(row.get("身份证号"))) {
                EmployeeVO employee = new EmployeeVO();
                employee.setUid(employeeVO.getUid());
                employee.setSex(employeeVO.getSex());
                employee.setBirthday(employeeVO.getBirthday());
                employeeService.edit(employee);
            }
            if (whetherToCreate) {
                throw new RuntimeException("该人员已存在");
            } else {
                return verify;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (whetherToCreate && !e.getMessage().equals("101") && !e.getMessage().equals("102")) {
                row.put("失败原因", e.getMessage());
                employeeVO.setImportError(true);
                return employeeVO;
            }
            List<CompanyDictDataVO> payLevelList = companyDictDataMapper.getCompanyModelType("employee_pay_level");

            if (StringUtils.isEmpty(row.get("消费等级"))) {
                for (CompanyDictDataVO payLevel : payLevelList) {
                    if ("默认等级".equals(payLevel.getDictLabel())) {
                        employeeVO.setPayLevel(payLevel.getDictValue());
                    }
                }
            } else {
                for (CompanyDictDataVO payLevel : payLevelList) {
                    if (payLevel.getDictLabel().equals(row.get("消费等级"))) {
                        employeeVO.setPayLevel(payLevel.getDictValue());
                    }
                    if (payLevel.getDictValue() == Integer.parseInt(row.get("消费等级"))) {
                        employeeVO.setPayLevel(payLevel.getDictValue());
                    }
                }
            }
            if (StringUtils.isEmpty(row.get("性别"))) {
                employeeVO.setSex(3);
            } else {
                List<SysDictDataVO> sexList = sysDictDataMapper.getListByDictType("sex");
                for (SysDictDataVO sex : sexList) {
                    if (sex.getDictLabel().equals(row.get("性别"))) {
                        employeeVO.setPayLevel(Integer.parseInt(sex.getDictValue()));
                    }
                    if (sex.getDictValue().equals(row.get("性别"))) {
                        employeeVO.setPayLevel(Integer.parseInt(sex.getDictValue()));
                    }
                }
            }
            employeeVO.setName(row.get("姓名"));
            employeeVO.setCompanyCode(companyCode);
            if (StringUtils.isNotEmpty(row.get("进入时间"))) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    employeeVO.setEnterDate(formatter.parse(row.get("进入时间")));
                } catch (ParseException e1) {
                    log.error("进入时间，类型转换异常", e1);
                }
            } else {
                employeeVO.setEnterDate(new Date());
            }
            if (StringUtils.isNotEmpty(row.get("生日")) || e.getMessage().equals("101")) {//身份证为空
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    employeeVO.setBirthday(formatter.parse((row.get("生日"))));
                } catch (ParseException e1) {
                    log.error("生日，类型转换异常", e1);
                }
            }
            employeeService.add(employeeVO);
            return employeeVO;
        }
    }

    /**
     * 人员的有效性 根据机构或商户的companyCode，在employee表中寻找名称匹配的employee对象,若没有则创建该人员
     *
     * @param employeeVO      当前数据
     * @param companyCode     当前商户code
     * @param whetherToCreate 是否创建人员
     * @param replace         是否更新人员信息
     * @param judgment        判重
     * @param empty           判空
     * @return
     * @throws Exception
     */
    @Override
    public EmployeeVO employeeIsItEffective(EmployeeVO employeeVO, String companyCode, boolean whetherToCreate, boolean replace, boolean judgment, boolean empty, AdminVO adminVO) {
        //插入人员(身份证号或编号,当前商户判重导入)
        employeeVO.setCompanyCode(companyCode);
        EmployeeVO verifyEmployee = employeeMapper.getVerifyEmployeeVO(employeeVO);
        if (verifyEmployee != null) {
            if (judgment) throw new RuntimeException("重复 : 数据库内重复");
            employeeVO.setUid(verifyEmployee.getUid());
            employeeVO.setStatus(verifyEmployee.getStatus());
        } else {
            if (empty) throw new RuntimeException("查询错误-该人员不存在-禁止修改人员信息");
            if (whetherToCreate) {
                EmployeeAccountRecordVO employeeAccountRecordVO = new EmployeeAccountRecordVO();
                try {
                    BeanConvertorUtil.reverseConvertor(employeeVO, employeeAccountRecordVO);
                    employeeAccountRecordVO.setEmployeeStatus(employeeVO.getStatus());
                    employeeAccountRecordVO.setEmployeeLabelDataUid(employeeVO.getLabelDataUid());
                    employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountOpen);//新增
                    employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());//新增
                    employeeAccountRecordService.createAccount(employeeAccountRecordVO, adminVO);//新增账户
                } catch (RuntimeException e) {
                    throw new RuntimeException(e.getMessage());
                } catch (Exception e) {
                    throw new RuntimeException(e.getMessage());
                }
                employeeVO.setUid(employeeAccountRecordVO.getEmployeeUid());
                return employeeVO;
            }
        }
        if (replace) {
            EmployeeAccountRecordVO employeeAccountRecordVO = new EmployeeAccountRecordVO();

            try {
                BeanConvertorUtil.reverseConvertor(employeeVO, employeeAccountRecordVO);
                employeeAccountRecordVO.setEmployeeUid(employeeVO.getUid());
                employeeAccountRecordVO.setEmployeeStatus(employeeVO.getStatus());
                employeeAccountRecordVO.setEmployeeLabelDataUid(employeeVO.getLabelDataUid());
                Employee employee = employeeService.getById(employeeAccountRecordVO.getEmployeeUid());
                if (employee.getDailyLimit() == employeeAccountRecordVO.getEmployeeDailyLimit()) {
                    employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountEdit);
                } else {
                    employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountModifyLimitEdit);
                }
                employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());//更新账户
                employeeAccountRecordService.editAccount(employeeAccountRecordVO, adminVO);//更新账户
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        return employeeVO;
    }

    /**
     * 单条导入
     *
     * @param importDetail       当前数据
     * @param importBatch        当前数据批次
     * @param adminVO            当前管理员
     * @param columnConfigMap    列配置
     * @param payLevelList
     * @param sexList
     * @param deptRoleTreeEntity
     * @return
     * @throws Exception
     */
    @Override
    public EmployeeVO singleImport(ImportDetail importDetail, ImportBatch importBatch, AdminVO adminVO, Map<String, String> columnConfigMap, List<CompanyDictData> payLevelList, List<SysDictDataVO> sexList, Map<String, DeptVO> deptRoleTreeEntity) {
        EmployeeVO employeeVO = new EmployeeVO();
        employeeVO.setCompanyUid(adminVO.getCompanyUid());

        Employee employee = null;//持卡人信息
        DeptVO deptVO = null;//机构信息
        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
            String key = entry.getValue().split(",")[0];//字段对应的名称
            String fieldName = entry.getKey();//字段名称
            String fieldValue = ClassUtil.getFieldValueByFieldName(fieldName, importDetail);
            //获取校验条件
            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                switch (EIBatchType.getByValue(importBatch.getType())) {
                    case employeeAccountOpen:
                        if ("身份证号".equals(key)) {
                            employeeVO.setCertCode(fieldValue);
                        }
                        if ("日限额".equals(key)) {
                            employeeVO.setDailyLimit(Double.parseDouble(fieldValue));
                        }
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(fieldValue);
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }
                            employeeVO.setCompanyCode(deptVO.getCompanyCode());
                        }
                        break;
                    case employeeAccountEdit:
                        if ("人员编号".equals(key)) {
                            employee = employeeService.getByCode(fieldValue);
                            if (employee == null) {
                                throw new RuntimeException(key + "不存在-请核对编号信息");
                            }
                            employeeVO.setEmployeeCode(fieldValue);
                            employeeVO.setUid(employee.getUid());
                        }
                        if ("身份证号".equals(key)) {
                            employeeVO.setCertCode(fieldValue);
                        }
                        if ("所属机构".equals(key)) {
                            deptVO = deptRoleTreeEntity.get(fieldValue);
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }
                            employeeVO.setCompanyCode(deptVO.getCompanyCode());
                        }
                        if ("日限额".equals(key)) {
                            employeeVO.setDailyLimit(Double.parseDouble(fieldValue));
                        }
                        break;
                }
            }
        }
        for (Map.Entry<String, String> entry : columnConfigMap.entrySet()) {
            String key = entry.getValue().split(",")[0];//字段对应的名称
            String fieldName = entry.getKey();//字段名称
            String fieldValue = ClassUtil.getFieldValueByFieldName(fieldName, importDetail);
            //进行校验赋值
            try {
                if (importDetail.getStatus() != 2) {
                    switch (key) {
                        case "上级机构":
                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                throw new RuntimeException(key + "为空");
                            }
                            if (deptVO == null) {
                                throw new RuntimeException("所属机构不存在");
                            }
                            if (fieldValue.split("/").length > deptVO.getSuperCompanyName().split("/").length && !fieldValue.equals((deptVO.getSuperCompanyName()) + "/" + deptVO.getName())) {
                                throw new RuntimeException(key + "上级机构信息错误");
                            }
                            if (fieldValue.split("/").length == deptVO.getSuperCompanyName().split("/").length && !fieldValue.equals(deptVO.getSuperCompanyName())) {
                                throw new RuntimeException(key + "上级机构信息错误");
                            }

                            if (fieldValue.split("/").length < deptVO.getSuperCompanyName().split("/").length && !(fieldValue + "/" + deptVO.getName()).equals(deptVO.getSuperCompanyName())) {
                                throw new RuntimeException(key + "上级机构信息错误");
                            }
                            break;
                        case "所属机构":
                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                throw new RuntimeException(key + "为空");
                            }
                            if (deptVO == null) {
                                throw new RuntimeException(key + "不存在");
                            }
                            break;
                        case "人员编号":
                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                throw new RuntimeException(key + "为空");
                            }
                            if (employee == null && employeeService.getByCode(fieldValue) != null) {
                                throw new RuntimeException(key + "重复-数据库重复");
                            }
                            if (employee == null) {
                                employeeVO.setEmployeeCode(fieldValue);
                            }
                            break;
                        case "姓名":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                employeeVO.setName(fieldValue);
                            } else {
                                if (employee != null) {
                                    employeeVO.setName(employee.getName());
                                } else {
                                    throw new RuntimeException(key + "为空");
                                }
                            }
                            break;
                        case "身份证号":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                if (employee == null && employeeService.getByCertCode(fieldValue) != null) {
                                    throw new RuntimeException(key + "重复-数据库重复");
                                }
                                employeeVO.setCertCode(fieldValue);
                            } else {
                                if (employee != null) {
                                    employeeVO.setCertCode(employee.getCertCode());
                                }
                            }
                            break;
                        case "性别":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                if (StringUtils.isNotEmpty(employeeVO.getCertCode()) && StringUtils.isNotEmpty(employeeVO.getCertCode().trim())) {
                                    if (!EmployeeUtil.genderIsItEffective(fieldValue, employeeVO.getCertCode()))
                                        throw new RuntimeException(key + "与身份证号的性别不匹配");
                                }
                                for (SysDictDataVO sex : sexList) {
                                    if (sex.getDictLabel().equals(fieldValue)) {
                                        employeeVO.setSex(Integer.parseInt(sex.getDictValue()));
                                    }
                                    if (sex.getDictValue().equals(fieldValue)) {
                                        employeeVO.setSex(Integer.parseInt(sex.getDictValue()));
                                    }
                                }
                            } else {
                                if (StringUtils.isNotEmpty(employeeVO.getCertCode()) && StringUtils.isNotEmpty(employeeVO.getCertCode().trim())) {
                                    employeeVO.setSex(Integer.parseInt(EmployeeUtil.getSexCode(employeeVO.getCertCode())));
                                }
                            }
                            break;
                        case "生日":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                employeeVO.setBirthday(DateUtils.stringToDate(fieldValue, "yyyy-MM-dd"));
                            } else {
                                if (StringUtils.isNotEmpty(employeeVO.getCertCode()) && StringUtils.isNotEmpty(employeeVO.getCertCode().trim())) {
                                    employeeVO.setBirthday(DateUtils.stringToDate(EmployeeUtil.birthdayBasedOnCertCode(employeeVO.getCertCode()), "yyyy-MM-dd"));
                                }
                            }
                            break;
                        case "进入时间":
                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                throw new RuntimeException(key + "为空");
                            }
                            employeeVO.setEnterDate(DateUtils.stringToDate(fieldValue, "yyyy-MM-dd"));
                            break;
                        case "账户有效时间":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                employeeVO.setValidDate(new Timestamp(DateUtils.strToDateTime(fieldValue).getTime()));
                            }
                            break;
                        case "人脸支付":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                employeeVO.setIsFacePayment("开".equals(fieldValue) ? 1 : 0);
                            }
                            break;
                        case "消费等级":
                            int pay = 0;
                            for (CompanyDictData payLevel : payLevelList) {
                                if (payLevel.getDictLabel().equals(fieldValue)) {
                                    employeeVO.setPayLevel(payLevel.getDictValue());
                                    pay++;
                                    break;
                                }
                            }
                            if (pay == 0) throw new RuntimeException(key + "不合法");
                            break;
                        case "日限额":
                            if (employeeVO.getDailyLimit() == 0) {
                                throw new RuntimeException(key + "为空");
                            }
                            break;
                        case "月限额":
                            if (StringUtils.isEmpty(fieldValue) || StringUtils.isEmpty(fieldValue.trim())) {
                                employeeVO.setMonthlyLimit(employeeVO.getDailyLimit() * 30);
                            } else {
                                if (Double.parseDouble(fieldValue) > (employeeVO.getDailyLimit() * 30)) {
                                    throw new RuntimeException(key + "超额");
                                }
                                employeeVO.setMonthlyLimit(Double.parseDouble(fieldValue));
                            }
                            break;
                        case "超限额密码":
                            if (StringUtils.isNotEmpty(fieldValue) && StringUtils.isNotEmpty(fieldValue.trim())) {
                                employeeVO.setDefaultPwd("000000" + fieldValue);
                            } else {
                                if (!employeeVO.getEmployeeCode().isEmpty()) {
                                    employeeVO.setDefaultPwd("000000" + employeeVO.getEmployeeCode());
                                }
                            }
                            employeeVO.setDefaultPwd(employeeVO.getDefaultPwd().substring(employeeVO.getDefaultPwd().length() - 6));
                            break;
                    }
                }
            } catch (RuntimeException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

        if (importDetail.getStatus() != 2) {
            EmployeeAccountRecordVO employeeAccountRecordVO = new EmployeeAccountRecordVO();
            switch (EIBatchType.getByValue(importBatch.getType())) {
                case employeeAccountOpen:
                    try {
                        BeanConvertorUtil.reverseConvertor(employeeVO, employeeAccountRecordVO);
                        employeeAccountRecordVO.setEmployeeStatus(1);
                        employeeAccountRecordVO.setEmployeeLabelDataUid(employeeVO.getLabelDataUid());
                        employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountOpen);//新增
                        employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());//新增
                        employeeAccountRecordService.createAccount(employeeAccountRecordVO, adminVO);//新增账户
                        employeeVO.setUid(employeeAccountRecordVO.getEmployeeUid());
                        importDetail.setStatus(2);
                    } catch (RuntimeException e) {
                        throw new RuntimeException(e.getMessage());
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                    break;
                case employeeAccountEdit:
                    try {
                        BeanConvertorUtil.reverseConvertor(employeeVO, employeeAccountRecordVO);
                        employeeAccountRecordVO.setEmployeeUid(employeeVO.getUid());
                        employeeAccountRecordVO.setEmployeeStatus(1);
                        employeeAccountRecordVO.setEmployeeLabelDataUid(employeeVO.getLabelDataUid());
                        employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountEdit);
                        employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());//更新账户
                        employeeAccountRecordService.editAccount(employeeAccountRecordVO, adminVO);//更新账户
                        importDetail.setStatus(2);
                    } catch (RuntimeException e) {
                        throw new RuntimeException(e.getMessage());
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                    break;
            }
        }
        return employeeVO;
    }

    @Override
    public String getAutoEmployeeCode() {
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        Integer ownerType = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
            ownerType = (Integer) request.getAttribute(SysConf.OWNER_TYPE);
        }
        if (ownerType == null || ownerUid == null) {
            AdminVO adminVO = adminService.getMe();
            ownerType = adminVO.getOwnerType();
            ownerUid = adminVO.getCompanyUid();
        }
        //获取自动生成人员编号
        SequenceVO sequenceVO = new SequenceVO();
        sequenceVO.setOwnerType(ownerType);
        sequenceVO.setOwnerUid(ownerUid);
        sequenceVO.setBusiType(EBusiType.employeeCode);
        sequenceVO.setCodeType(EBusinessType.employeeAutoCode);
        Sequence sequence = sequenceService.customizeSelect(sequenceVO);
        String code = sequenceService.generateSerialCode(sequence);
        return code;
    }

    @Override
    public String getAutoEmployeeCode(String companyUid) {
        if (companyUid == null) {
            throw new RuntimeException("系统错误无法找到当前用户所属信息");
        }
        //获取自动生成人员编号
        SequenceVO sequenceVO = new SequenceVO();
        sequenceVO.setOwnerType(2);
        sequenceVO.setOwnerUid(companyUid);
        sequenceVO.setBusiType(EBusiType.employeeCode);
        sequenceVO.setCodeType(EBusinessType.employeeAutoCode);
        Sequence sequence = sequenceService.customizeSelect(sequenceVO);
        String code = sequenceService.generateSerialCode(sequence);
        return code;
    }

    @Override
    public String batchModifyLimit(EmployeeVO employeeVO) {
        if (employeeVO.getEmployeeVOList().isEmpty()) {
            QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
            queryWrapper.likeRight("company_code", DeptUtil.getLatestCompnay(employeeVO.getCompanyCode()));
            Page<Employee> page = new Page<>();
            page.setCurrent(1L);
            page.setSize(1000);
            queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
            IPage<Employee> pageList = employeeService.page(page, queryWrapper);
            List<Employee> list = pageList.getRecords();
            for (Employee employee : list) {
                EmployeeVO employeeVO1 = new EmployeeVO();
                BeanUtils.copyProperties(employee, employeeVO1);
                employeeVO1.setDailyLimit(employeeVO.getDailyLimit());
                employeeVO1.setMonthlyLimit(employeeVO.getMonthlyLimit());
                edit(employeeVO1);
            }
        } else {
            for (EmployeeVO employee : employeeVO.getEmployeeVOList()) {
                Employee employeeServiceById = employeeService.getById(employee.getUid());
                BeanUtils.copyProperties(employeeServiceById, employee);
                employee.setDailyLimit(employeeVO.getDailyLimit());
                employee.setMonthlyLimit(employeeVO.getMonthlyLimit());
                edit(employee);
            }
        }

        return ResultUtil.successWithMessage("更新成功");
    }

    @Override
    public Boolean exitCertCode(String companyCode, String certCode) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cert_code", certCode.trim());
        queryWrapper.eq("status", 1);
        queryWrapper.likeRight("company_code", DeptUtil.getLatestCompnay(companyCode.trim()));
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        Page<Employee> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        IPage<Employee> pageList = employeeService.page(page, queryWrapper);
        List<Employee> records = pageList.getRecords();
        return pageList.getRecords().size() > 0;
    }

    @Override
    public String restore(EmployeeVO employeeVO) {
        if (employeeVO.getUid() == null && employeeVO.getUid() != null) {
            employeeVO.setUid(employeeVO.getUid());
        }
        Employee entity = employeeService.getById(employeeVO.getUid());
        /* Date LeaveDate = new Date();*/
        if (entity.getStatus() == 1) {
            throw new RuntimeException("该持卡人未离开");
        }
        entity.setStatus(EStatus.ENABLE);
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.updateById();
        BeanUtils.copyProperties(employeeVO, entity);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String batchRestore(List<EmployeeVO> employeeVOS) {

        int error = 0;
        for (EmployeeVO employeeVO : employeeVOS) {
            try {
                employeeService.restore(employeeVO);
            } catch (Exception e) {
                e.printStackTrace();
                if (e.getMessage() == "该持卡人未注销") {
                    error += 1;
                }
                return ResultUtil.errorWithMessage("批量恢复失败");
            }
        }
        return ResultUtil.successWithMessage("成功恢复" + (employeeVOS.size() - error) + ",恢复失败" + error);
    }

    @Override
    public EmployeeVO certCodeTrans(EmployeeVO employeeVO) {
        java.sql.Date parse = null;
        try {
            if (employeeVO.getCertCode().length() == 18) {
                int sex = (Integer.parseInt(employeeVO.getCertCode().substring(16).substring(0, 1)) % 2 == 0 ? 2 : 1);
                employeeVO.setSex(sex);
                String year = employeeVO.getCertCode().substring(6).substring(0, 4);
                String month = employeeVO.getCertCode().substring(10).substring(0, 2);
                String day = employeeVO.getCertCode().substring(12).substring(0, 2);
                String birthday = year + "-" + month + "-" + day;
                parse = java.sql.Date.valueOf(birthday);
            } else if (employeeVO.getCertCode().length() == 15) {
                int sex = (Integer.parseInt(employeeVO.getCertCode().substring(14).substring(0, 1)) % 2 == 0 ? 2 : 1);
                employeeVO.setSex(sex);
                String year = "19" + employeeVO.getCertCode().substring(6, 8);
                String month = employeeVO.getCertCode().substring(8, 10);
                String day = employeeVO.getCertCode().substring(10, 10);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                String birthday = year + "-" + month + "-" + day;
                parse = java.sql.Date.valueOf(birthday);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            employeeVO.setImportError(true);
            return employeeVO;
        }
        employeeVO.setBirthday(new Date(parse.getTime()));
        return employeeVO;
    }
    @Override
    public EmployeeVO getEmployeeDetailForMobile(EmployeeVO employeeVO) {
        return employeeMapper.getEmployeeDetailForMobile(employeeVO);
    }

    @Override
    public String editAdmin(EmployeeVO employeeVO) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_uid", employeeVO.getAdminUid());
        queryWrapper.in("company_code", employeeVO.getDeptRoleTree());
        if (employeeService.count(queryWrapper) > 0) {
            throw new RuntimeException("请先撤销已有权限持卡人的绑定");
        }
        Employee entity = employeeService.getById(employeeVO.getUid());
        if (entity.getAdminUid() != null) {
            entity.setAdminUid(null);
        } else {
            entity.setAdminUid(employeeVO.getAdminUid());
        }
        CompanyVO companyByEmployeeUid = companyMapper.getCompanyByEmployeeUid(entity.getCompanyCode());
        Integer number = null;
        try {
            number = sequenceService.getVariousCode(companyByEmployeeUid.getUid(), 2, EBusinessType.employeeSyncCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        entity.setSyncCode(number);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<EmployeeVO> getAdminBindEmployeeList(EmployeeVO employeeVO) {
        List<EmployeeVO> list = employeeMapper.getAllListByCompany(employeeVO);
        return list;
    }

    @Override
    public Employee getByCode(String value) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_code", value);
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.last(SysConf.LIMIT_ONE);
        Employee employee = employeeService.getOne(queryWrapper);
        return employee;
    }

    @Override
    public Employee getByCertCode(String value) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cert_code", value);
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.last(SysConf.LIMIT_ONE);
        Employee employee = employeeService.getOne(queryWrapper);
        return employee;
    }

    @Override
    public List<Employee> getAllEmployeeListByCompanyCode(String companyCode) {
        return employeeMapper.getAllEmployeeListByCompanyCode(companyCode);
    }

    @Override
    public String HandelChangeRecord(EmployeeVO employeeVO) {
        if (employeeVO.getChangeRecord() == 1) {//变动记录
            EmployeeDeptRecordVO employeeDeptRecordVO = new EmployeeDeptRecordVO();
            employeeDeptRecordVO.setEmployeeUid(employeeVO.getUid());
            employeeDeptRecordVO.setNewDeptUid(employeeVO.getNewDeptUid());
            employeeDeptRecordVO.setOldDeptUid(employeeVO.getOldDeptUid());
            employeeDeptRecordVO.setReason(employeeVO.getReason());
            employeeDeptRecordVO.setHasEmployeeRecord(employeeVO.getHasEmployeeRecord());
            employeeDeptRecordService.add(employeeDeptRecordVO);//直接生成变动记录
        }
        if (employeeVO.getHasEmployeeRecord() == 1) {//简历
            QueryWrapper<EmployeeRecord>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("employee_uid",employeeVO.getUid());
            queryWrapper.orderByDesc("create_time");
            queryWrapper.last("limit 0,1");
            EmployeeRecord eRecord = employeeRecordService.getOne(queryWrapper);
            if(eRecord != null){
                eRecord.setEndTime(new Date());
                eRecord.updateById();
            }
            EmployeeRecordVO employeeRecordVO = new EmployeeRecordVO();
            employeeRecordVO.setEmployeeUid(employeeVO.getUid());
            employeeRecordVO.setSourceUid(employeeVO.getOldDeptUid());
            employeeRecordVO.setType(1);
            employeeRecordVO.setFiles(employeeVO.getFiles());
            employeeRecordVO.setContent(employeeVO.getContent());
            Dept deptSummary = deptService.getById(employeeVO.getNewDeptUid());
            employeeRecordVO.setSummary(deptSummary.getName());
            employeeRecordVO.setFiles(employeeVO.getFiles());
            employeeRecordVO.setBeginTime(employeeVO.getVitaBeginTime());
            employeeRecordVO.setEndTime(employeeVO.getVitaEndTime());
            employeeRecordService.add(employeeRecordVO);
        }
        return ResultUtil.successWithMessage("记录生成成功");
    }

    @Override
    public List<EmployeeVO> getTeacherInfo(EmployeeVO employeeVO) {
        return employeeMapper.getTeacherInfo(employeeVO);
    }

    @Override
    public List<EmployeeVO> getGradeTeacherInfo(EmployeeVO employeeVO) {
        return employeeMapper.getGradeTeacherInfo(employeeVO);
    }

    @Override
    public String editEmployeeChange(EmployeeVO employeeVO) {
        if(employeeVO.getUid() == null){
            if(employeeVO.getEmployeeVOList() == null || employeeVO.getEmployeeVOList().size() == 0){
                throw new RuntimeException("批量处理数据不可为空");
            }
            for (EmployeeVO employee : employeeVO.getEmployeeVOList()) {
                employee.setCompanyCode(employeeVO.getCompanyCode());
                employee.setReason(employeeVO.getReason());
                employee.setVitaBeginTime(employeeVO.getVitaBeginTime());
                employee.setVitaEndTime(employeeVO.getVitaEndTime());
                employee.setChangeRecord(employeeVO.getChangeRecord());
                employee.setHasEmployeeRecord(employeeVO.getHasEmployeeRecord());
                employee.setFiles(employeeVO.getFiles());
                employee.setContent(employeeVO.getContent());
                Employee entity = employeeService.getById(employee.getUid());
                Dept oldDept = deptService.getDeptByCode(entity.getCompanyCode());
                edit(employee);
                Dept newDept = deptService.getDeptByCode(employee.getCompanyCode());
                employee.setOldDeptUid(oldDept.getUid());
                employee.setNewDeptUid(newDept.getUid());
                employeeService.HandelChangeRecord(employee);
            }
        }else {
            Employee employee = employeeService.getById(employeeVO.getUid());
            Dept oldDept = deptService.getDeptByCode(employee.getCompanyCode());
            edit(employeeVO);
            Dept newDept = deptService.getDeptByCode(employeeVO.getCompanyCode());
            employeeVO.setOldDeptUid(oldDept.getUid());
            employeeVO.setNewDeptUid(newDept.getUid());
            return employeeService.HandelChangeRecord(employeeVO);
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public Employee getByCode(String companyCode, String employeeCode) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_code", employeeCode);
        queryWrapper.likeRight("company_code",companyCode);
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        queryWrapper.last(SysConf.LIMIT_ONE);
        Employee employee = employeeService.getOne(queryWrapper);
        return employee;
    }

    @Override
    public String editNameSake(EmployeeVO employeeVO) {
        Employee entity = employeeService.getById(employeeVO.getUid());
        entity.setNameEndWith(employeeVO.getNameEndWith());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<EmployeeVO> getEmployeeListByDeptUid(String deptUid) {
        return employeeMapper.getEmployeeListByDeptUid(deptUid);
    }

    @Override
    public List<EmployeeVO> getTeacherMesByCodeAndNameOrCertCode(EmployeeVO employeeVO) {
        Dept dept = gradeService.getDeptByUid(employeeVO.getGradeUid());
        if(dept != null){
            int level = Integer.parseInt(DeptUtil.getCompanyLevel(dept.getCompanyCode()));
            String companyCode = DeptUtil.getCompanyFull(DeptUtil.getCompanyShortCode(dept.getCompanyCode(),String.valueOf(level == 0?0:level-1)));
            employeeVO.setCompanyCode(companyCode);
            return employeeMapper.getTeacherMesByCodeAndNameOrCertCode(employeeVO);
        }else {
            return new ArrayList<>();
        }
    }
    @Override
    public List<EmployeeVO> getEmployeeMesByCodeAndNameOrCertCode(EmployeeVO employeeVO) {
        return employeeMapper.getEmployeeMesByCodeAndNameOrCertCode(employeeVO);
    }

    @Override
    public EmployeeVO getEmployeeMesByCodeAndNameOrCertCodeAndSuffix(EmployeeVO employeeVO) {
        return employeeMapper.getEmployeeMesByCodeAndNameOrCertCodeAndSuffix(employeeVO);
    }

    @Override
    public List<EmployeeVO> getEmployeeAllListByGrade(String gradeUid) {
        return employeeMapper.getEmployeeAllListByGrade(gradeUid);
    }

    @Override
    public List<EmployeeVO> getGradeStudentInfo(EmployeeVO employeeVO) {
        return employeeMapper.getGradeStudentInfo(employeeVO);
    }

    @Override
    public List<EmployeeVO> getAdminBindTeacherList(EmployeeVO employeeVO) {
        return employeeMapper.getAdminBindTeacherList(employeeVO);
    }

    @Override
    public List<EmployeeVO> getNoGradeTeacherMesByCodeAndNameOrCertCode(EmployeeVO employeeVO) {
        return employeeMapper.getNoGradeTeacherMesByCodeAndNameOrCertCode(employeeVO);
    }

}
